//
//  UserHeaderView.m
//  ROSE
//
//  Created by 艾阳 on 2018/5/31.
//  Copyright © 2018年 HeNanZhongDunYunAn. All rights reserved.
//

#import "UserHeaderView.h"

@implementation UserHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor themeColor];
        if (!_headImg) {
            _headImg = [[UIImageView alloc]init];
            _headImg.contentMode = UIViewContentModeScaleToFill;
            //UIViewContentModeScaleAspectFill;
            _headImg.layer.masksToBounds = YES;
            _headImg.layer.cornerRadius = 40.0f;
            [_headImg setImage:[UIImage imageNamed:@"headImg"]];
            [self addSubview:_headImg];
        }
        if(!_Name){
            _Name = [[UILabel alloc]init];
            _Name.backgroundColor = [UIColor clearColor];
            _Name.textColor = [UIColor whiteColor];
            _Name.textAlignment = NSTextAlignmentLeft;
            _Name.font = [UIFont boldSystemFontOfSize:18];
            _Name.text = @"小王子";
            [self addSubview:_Name];
        }
        if(!_account){
            _account = [[UILabel alloc]init];
            _account.backgroundColor = [UIColor clearColor];
            _account.textColor = [UIColor whiteColor];
            _account.textAlignment = NSTextAlignmentLeft;
            _account.font = [UIFont systemFontOfSize:15];
            _account.text = @"THE ROSE @126.com";
            //_account.backgroundColor = [UIColor yellowColor];
            [self addSubview:_account];
        }
        if(!_energy){
            _energy = [[UILabel alloc]init];
            _energy.backgroundColor = [UIColor clearColor];
            _energy.textColor = [UIColor whiteColor];
            _energy.textAlignment = NSTextAlignmentLeft;
            _energy.font = [UIFont systemFontOfSize:13];
            _energy.text = @"能量:56539";
            [self addSubview:_energy];
        }
        
        if (!_charmValue) {
            _charmValue = [[UILabel alloc]init];
            _charmValue.backgroundColor = [UIColor clearColor];
            _charmValue.textColor = [UIColor whiteColor];
            _charmValue.textAlignment = NSTextAlignmentCenter;
            [_charmValue setAttributedText:[self changeLabelWithText:@"56539.2365\n魅力值 "]];
            _charmValue.numberOfLines = 2;
            [self addSubview:_charmValue];
        }
        if (!_ETHBalance) {
            _ETHBalance = [[UILabel alloc]init];
            _ETHBalance.backgroundColor = [UIColor clearColor];
            _ETHBalance.textColor = [UIColor whiteColor];
            _ETHBalance.textAlignment = NSTextAlignmentCenter;
            [_ETHBalance setAttributedText:[self changeLabelWithText:@"35900.00000023 \nETH值"]];
            
            _ETHBalance.numberOfLines = 2;
            [self addSubview:_ETHBalance];
        }
        
        if(!_detailBtn){
            _detailBtn = [[UIButton alloc]init];
            _detailBtn.backgroundColor = [UIColor clearColor];
            [_detailBtn setImage:[UIImage imageNamed:@"右箭头"] forState:UIControlStateNormal];
            _detailBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
           // _detailBtn.backgroundColor = [UIColor blueColor];
            [self addSubview:_detailBtn];
        }
        
        [self makeLayout];
        [self setHeaderData];
    }
    return self;
}

//创建一个返回富文本的方法
-(NSMutableAttributedString*) changeLabelWithText:(NSString*)needText

{
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:needText];
    
    UIFont *font1 = [UIFont systemFontOfSize:18];
    UIFont *font2 = [UIFont systemFontOfSize:13];
    //0123456789 length=10
    [attrString addAttribute:NSFontAttributeName value:font1 range:NSMakeRange(0,needText.length-5)];
    [attrString addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(needText.length-4,4)];
    return attrString;
    
}


- (void)makeLayout{
    [self.headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
        make.top.mas_equalTo(16);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
    }];
    [self.Name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headImg.mas_right).with.offset(10);
        make.top.mas_equalTo(self.headImg.mas_top).with.offset(5);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(20);
    }];
    [self.account mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headImg.mas_right).with.offset(10);
        make.top.mas_equalTo(self.Name.mas_bottom).with.offset(5);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(20);
    }];
    [self.energy mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headImg.mas_right).with.offset(10);
        make.top.mas_equalTo(self.account.mas_bottom).with.offset(5);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(20);
    }];
    
    [self.charmValue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(self.headImg.mas_bottom).with.offset(20);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(size_width/2);
    }];
    
    [self.ETHBalance mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(self.headImg.mas_bottom).with.offset(20);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(size_width/2);
    }];
    
    [self.detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headImg.mas_top);
        make.bottom.mas_equalTo(self.headImg.mas_bottom);
        make.right.mas_equalTo(0);
        make.width.mas_equalTo(80);
    }];
    
    
}

- (void)setHeaderData{
    UserInfoModel *userInfo = [UserInfoModel sharedInstance];
    self.Name.text = userInfo.YHM;
    self.account.text = userInfo.ZH;
    self.energy.text = [NSString stringWithFormat:@"能量：%@",userInfo.MFNL];
    //self.charmValue.text = userInfo.YUE;
    [self.charmValue setAttributedText:[self changeLabelWithText:[NSString stringWithFormat:@"%@\n魅力值 ",userInfo.YUE]]];
}

@end
